//
//  PayTableViewCell.swift
//  CJXSwiftStoryboard
//
//  Created by CJX on 2016/10/26.
//  Copyright © 2016年 caijianxi. All rights reserved.
//

import UIKit

protocol PayTableViewCellDelegate {
    func monthDelegateToPayVC()
    func yearDelegateToPayVC()
}

class PayTableViewCell: UITableViewCell,UITextFieldDelegate {

    var delegate : PayTableViewCellDelegate?
    @IBOutlet weak var CardView: UIView!
    
    @IBOutlet weak var CardTextField: UITextField!
    
    
    @IBOutlet weak var CVCView: UIView!
    
    
    @IBOutlet weak var CVCTextField: UITextField!
    
    
    @IBOutlet weak var monthBtn: UIButton!
    
    
    @IBOutlet weak var yearBtn: UIButton!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        CardView.layer.borderColor = UIColor.grayColor().CGColor
        CardView.layer.borderWidth = 1
        
        CVCView.layer.borderColor = UIColor.grayColor().CGColor
        CVCView.layer.borderWidth = 1
        
        monthBtn.layer.borderColor = UIColor.grayColor().CGColor
        monthBtn.layer.borderWidth = 1
        if SCREEN_WIDTH==320 {
            monthBtn.titleLabel?.font = UIFont.systemFontOfSize(15)
        }else{
            monthBtn.titleLabel?.font = UIFont.systemFontOfSize(17)
        }
        
        
        yearBtn.layer.borderColor = UIColor.grayColor().CGColor
        yearBtn.layer.borderWidth = 1
        if SCREEN_WIDTH==320 {
            yearBtn.titleLabel?.font = UIFont.systemFontOfSize(15)
        }else{
            yearBtn.titleLabel?.font = UIFont.systemFontOfSize(17)
        }
        
        CardTextField.borderStyle = .None
        CardTextField.keyboardType = .NumberPad
        CVCTextField.borderStyle = .None
        CVCTextField.keyboardType = .NumberPad
        
        CardTextField.addTarget(self, action: #selector(limitTextFieldLength), forControlEvents: .EditingChanged)
        CardTextField.delegate = self
        CVCTextField.addTarget(self, action: #selector(minTextFieldLength), forControlEvents: .EditingChanged)
        CVCTextField.delegate = self
        // Initialization code
    }

    
    func limitTextFieldLength(){
        if CardTextField.text?.characters.count >= 16 {

//            let range = NSMakeRange(0, 16)
//            
//            CardTextField.text = CardTextField.text?.substringWithRange(<#T##aRange: Range<Index>##Range<Index>#>)
            CardTextField.resignFirstResponder()
        }
    }
    
    func minTextFieldLength(){
        if CVCTextField.text?.characters.count >= 3 {
            CVCTextField.resignFirstResponder()
        }
    }
    

    
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        
        if textField == CardTextField {
            
            
            } else {
            
        }
        

        
        return true
    }
    
    @IBAction func monthBtnClick(sender: AnyObject) {
        
        self.delegate?.monthDelegateToPayVC()
    }
    
    @IBAction func YearBtnClick(sender: AnyObject) {
        
        self.delegate?.yearDelegateToPayVC()
    }
    
    
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
